package cc.eddic.pratice.programmingparadigms.streampractice.impl;

import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.impl.MasterDataServiceTyqImpl;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAccuracy;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAndAnswer;
import cc.eddic.pratice.programmingparadigms.streampractice.StreamPractice;

import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class QuestionAndAnswerTyqImpl implements StreamPractice {

    @Override
    public List<QuestionAndAnswer> mapToQuestionAndAnswer(List<Question> questionList) {
        List<QuestionAndAnswer> questionAndAnswers=new ArrayList<>();
        MasterDataServiceTyqImpl masterDataServiceTyq=new MasterDataServiceTyqImpl();
        for(Question question:questionList){
            Option option= masterDataServiceTyq.find(question.getOptions(), new Predicate<Option>() {
                @Override
                public boolean test(Option option) {
                    return option.isCorrectAnswer();
                }
            }).orElseThrow(()->new IllegalArgumentException("找不到第"+question.getNo()+"题的正确答案"));
            questionAndAnswers.add(new QuestionAndAnswer(question.getNo(),question.getDescription(),option.getChoice(), option.getAnswer()));
        }
        return questionAndAnswers;
    }


    @Override
    public List<QuestionAccuracy> calculateAccuracy(List<Answer> answerList) {
        //计算参加考试总人数
        Set<String> studentCount=new HashSet<>();
        for(Answer answer:answerList){
            studentCount.add(answer.getStudent().getName());
        }
        int num=studentCount.size();
        //计算每道题答对人数
        //存题号以及答对题数
        Map<Integer,Integer> rightQuestionnum=new HashMap<>();
        //存题号以及描述
        Map<Integer,String> questionMap=new HashMap<>();
        for(Answer answer:answerList){
            if(!rightQuestionnum.containsKey(answer.getQuestion().getNo())){
                questionMap.put(answer.getQuestion().getNo(),answer.getQuestion().getDescription());
                rightQuestionnum.put(answer.getQuestion().getNo(),0);
            }
            Option option=answer.getQuestion().getOptions().stream()
                    .filter(Option::isCorrectAnswer)
                    .findFirst()
                    .orElseThrow(()->new IllegalArgumentException("找不到第"+answer.getQuestion().getNo()+"题的正确答案"));
            if(answer.getChoice()==option.getChoice()){
                rightQuestionnum.put(answer.getQuestion().getNo(),rightQuestionnum.get(answer.getQuestion().getNo())+1);
            }
        }
        List<QuestionAccuracy> questionAccuracies=new ArrayList<>();
        for(Integer questionNum:questionMap.keySet()){
            questionAccuracies.add(new QuestionAccuracy(questionNum,questionMap.get(questionNum), (double)(rightQuestionnum.get(questionNum))/num));
            System.out.println(questionNum+"."+questionMap.get(questionNum)+(double)(rightQuestionnum.get(questionNum))/num);
        }
        return questionAccuracies;
    }
}
